<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class EditDirPage {

	private $alerter;
	
	public function __construct() {
		$this->alerter = new Alerter();
	}

	/**
	 * adds extra access checks, dependent on page state
	 */
	function page_inc_check_access($page) {
		if (!has_access("MODERATE") and !has_access("ADMIN"))
			errorpage(5);
	}

	/**
	 * appropriate page action interpretations
	 */
	function page_inc_execute_action($action) {
		global $new;
		switch($action) {
			case "add":
				check_access("ADMIN");
				$new = 1;
				break;
			default:
				$this->alerter->add_alert("unknown action");
				break;
		}
	}

	/**
	 * execute page logic
	 */
	function page_inc_execute($page) {
		global $CONFIG, $new;
		$creator = new FormCreator();
		$CONTENT = array();
		$THISDIR = $page->directory_tree[$page->dir_depth-1];
		
		$CONTENT['title'] = $new ? "Een directory toevoegen" : "Directory #{$THISDIR['dir_id']} aanpassen";
		
		if ($_POST['submitted']) {
		
			if ($_POST['delete']) {
				if (!$_POST['confirmed']) {
					$CONTENT['form'] = $creator->confirm("Bent u zeker dat deze directory weg moet?");
					$page->content = $CONTENT;
					return;
				} else if ($_POST['confirmed'] == "YES") {
					$sql = db_query_delete("DIRECTORY_TREE", "parent_id = {$THISDIR['parent_id']} AND child_id = {$THISDIR['dir_id']}");
					db_do_query($page, $sql);
					redirect("/home");
				}
				$_POST['delete'] = 0;
				
			} else {
			
				$checker = new InputChecker;	
				// input checks
				$ok = 1;
				if ($new) {
					$ok = ($checker->check_tags($_POST['name'], "naam") and $ok) ? 1 : 0;
					$ok = ($checker->check_exists($_POST['name'], "naam") and $ok) ? 1 : 0;
				}
				$ok = ($checker->check_tags($_POST['menuname'], "menunaam") and $ok) ? 1 : 0;
				$ok = ($checker->check_exists($_POST['menuname'], "menunaam") and $ok) ? 1 : 0;
				$ok = ($checker->check_tags($_POST['title'], "titel") and $ok) ? 1 : 0;
				$ok = ($checker->check_exists($_POST['title'], "titel") and $ok) ? 1 : 0;
				$ok = ($checker->check_tags($_POST['info'], "info") and $ok) ? 1 : 0;
				$ok = ($checker->check_exists($_POST['info'], "info") and $ok) ? 1 : 0;
				if ($_POST['show_body']) {
					$ok = ($checker->check_tags_advanced($_POST['body_main'], "body") and $ok) ? 1 : 0;
					$ok = ($checker->check_tag_closures($_POST['body_main']) and $ok) ? 1 : 0;
				}
				if ($_POST['body_footer']) {
					$ok = ($checker->check_tags_advanced($_POST['body_footer'], "footer") and $ok) ? 1 : 0;
					$ok = ($checker->check_tag_closures($_POST['body_footer']) and $ok) ? 1 : 0;
				}
				if ($new or ($_POST['relocate'] == "move") or ($_POST['relocate'] == "copy"))
					if ($_POST['location'] == "xxx") {
						$ok = 0;
						$this->alerter->add_alert("Geen locatie aangeduid");
					}
					
				if ($ok) {
				
					list($parent_id, $rank) = explode("#", $_POST['location']);
					
					if ($new) {
					
						// insert directory
						$body = "NULL";
						if ($_POST['body_main']) {
							$sql = db_query_insert(array('body'), array(db_sqlize($_POST['body_main'])), "BODY");
							db_do_query($page, $sql);
							$body = db_insert_id();
						}
						
						$footer = "NULL";
						if ($_POST['body_footer']) {
							$sql = db_query_insert(array('body'), array(db_sqlize($_POST['body_footer'])), "BODY");
							db_do_query($page, $sql);
							$footer = db_insert_id();
						}
						
						$sql = db_query_insert(array('name', 'menuname', 'title', 'info', 'body_main', 'body_footer', 'sorting_id', 'show_body', 'show_subsets', 'show_items', 'show_subfora', 'allow_new_subs', 'crop_topiclist'), array(strtolower(db_sqlize($_POST['name'])), db_sqlize($_POST['menuname']), db_sqlize($_POST['title']), db_sqlize($_POST['info']), $body, $footer, $_POST['sorting_id'], $_POST['show_body'] ? 1 : 0, $_POST['show_subsets'] ? 1 : 0, $_POST['show_items'] ? 1 : 0, $_POST['show_subfora'] ? 1 : 0, $_POST['allow_new_subs'] ? 1 : 0, $_POST['crop_topiclist'] ? 1 : 0), "DIRECTORY");
						db_do_query($page, $sql);
						$dir_id = db_insert_id();
						
						// insert tree listing
						$sql = db_query_update(array('rank'), array('rank+1'), "DIRECTORY_TREE", "parent_id = $parent_id AND rank >= $rank");
						db_do_query($page, $sql);
						$sql = db_query_insert(array('parent_id', 'child_id', 'rank'), array($parent_id, $dir_id, $rank), "DIRECTORY_TREE");
						db_do_query($page, $sql);
						
					} else {
						
						// update directory
						if ($page->body_main_id > 0) {
							$body = $page->body_main_id;
							$sql = db_query_update(array('body'), array(db_sqlize($_POST['body_main'])), "BODY", "body_id = $body");
							db_do_query($page, $sql);
						} else if ($_POST['body_main']) {
							$sql = db_query_insert(array('body'), array(db_sqlize($_POST['body_main'])), "BODY");
							db_do_query($page, $sql);
							$body = db_insert_id();
						} else
							$body = "NULL";
							
						if ($page->body_footer_id > 0) {
							$footer = $page->body_footer_id;
							$sql = db_query_update(array('body'), array(db_sqlize($_POST['body_footer'])), "BODY", "body_id = $footer");
							db_do_query($page, $sql);
						} else if ($_POST['body_footer']) {
							$sql = db_query_insert(array('body'), array(db_sqlize($_POST['body_footer'])), "BODY");
							db_do_query($page, $sql);
							$footer = db_insert_id();
						} else
							$footer = "NULL";
							
						$sql = db_query_update(array('menuname', 'title', 'info', 'body_main', 'body_footer', 'sorting_id', 'show_body', 'show_subsets', 'show_items', 'show_subfora', 'allow_new_subs', 'crop_topiclist'), array(db_sqlize($_POST['menuname']), db_sqlize($_POST['title']), db_sqlize($_POST['info']), $body, $footer, $_POST['sorting_id'], $_POST['show_body'] ? 1 : 0, $_POST['show_subsets'] ? 1 : 0, $_POST['show_items'] ? 1 : 0, $_POST['show_subfora'] ? 1 : 0, $_POST['allow_new_subs'] ? 1 : 0, $_POST['crop_topiclist'] ? 1 : 0), "DIRECTORY", "dir_id = {$_POST['dir_id']}");
						db_do_query($page, $sql);
						
						// update/insert tree listing
						switch($_POST['relocate']) {
							case "none":
								break;
							case "move":
								$sql = db_query_update(array('rank'), array('rank+1'), "DIRECTORY_TREE", "parent_id = $parent_id AND rank >= $rank");
								db_do_query($page, $sql);
								$sql = db_query_update(array('parent_id', 'rank'), array($parent_id, $rank), "DIRECTORY_TREE", "parent_id = {$THISDIR['parent_id']} AND child_id = {$_POST['dir_id']}");
								db_do_query($page, $sql);
								break;
							case "copy":
								$sql = db_query_update(array('rank'), array('rank+1'), "DIRECTORY_TREE", "parent_id = $parent_id AND rank >= $rank");
								db_do_query($page, $sql);
								$sql = db_query_insert(array('parent_id', 'child_id', 'rank'), array($parent_id, $_POST['dir_id'], $rank), "DIRECTORY_TREE");
								db_do_query($page, $sql);
								break;
						}
					
					}
					redirect($page->dir_path);
				}
		
			}
		}

		$fields[] = $creator->input_hidden("dir_id", $new ? "NEW" : $THISDIR['dir_id']);
		
		if ($new)
			$fields[] = $creator->input_field("Naam", "name", "");
		
		$fields[] = $creator->input_field("Naam in menu", "menuname", $new ? "" : db_desqlize($THISDIR['menuname']), "", 45); //TODO: remove magic number
		$fields[] = $creator->input_field("Titel", "title", $new ? "" : db_desqlize($THISDIR['title']), "", 70); //TODO: remove magic number 
		$fields[] = $creator->input_field("Info", "info", $new ? "" : db_desqlize($THISDIR['info']), "", 245); //TODO: remove magic number
		
		$sql = db_query_select(array(array('*')), array("SORTING"), 1);
		$sqlresult = db_do_query($page, $sql);
		$keys = array();
		$vals = array();
		while ($r = db_fetch_array($sqlresult)) {
			array_push($keys, $r['sorting_id']);
			array_push($vals, $r['sorting_name']);
		}
		$fields[] = $creator->input_select("Sortering", "sorting_id", $keys, $vals, "kies een sortering", $new ? 1 : $THISDIR['sorting_id']);

		$fields[] = $creator->input_checkbox("Body tonen", "show_body", $new ? 1 : $THISDIR['show_body']);
		$fields[] = $creator->input_checkbox("Subsets tonen", "show_subsets", $new ? 1 : $THISDIR['show_subsets']);
		$fields[] = $creator->input_checkbox("Items tonen", "show_items", $new ? 1 : $THISDIR['show_items']);
		$fields[] = $creator->input_checkbox("Subfora tonen", "show_subfora", $new ? 1 : $THISDIR['show_subfora']);
		$fields[] = $creator->input_checkbox("Gebruikers mogen eigen subsets toevoegen", "allow_new_subs", $new ? 1 : $THISDIR['allow_new_subs']);
		$fields[] = $creator->input_checkbox("Topiclijst limiteren tot {$CONFIG['numtopics']} topics/subset", "crop_topiclist", $new ? 1 : $THISDIR['crop_topiclist']);
		
		$fields[] = $creator->input_text("Body", "body_main", $new ? "" : db_desqlize($page->body_main));
		$fields[] = $creator->input_text("Footer", "body_footer", $new ? "" : db_desqlize($page->body_footer));
		
		// menu (re)location only for admins
		if (has_access("ADMIN")) {
			
			if (!$new)
				$fields[] = $creator->input_select("Actie", "relocate", array("none", "move", "copy"), array("none", "move", "copy"), "", "none");
			
			$keys = array();
			$vals = array();
			
			list($keys, $vals) = generate_list(0, 0, $keys, $vals, $page->menu);
				
			$fields[] = $creator->input_select("Locatie", "location", $keys, $vals);
			
			if (!$new)
				$fields[] = $creator->input_checkbox("Verwijder deze directory", "delete");
			
		}
		
		$CONTENT['form'] = $creator->create("dirform", array_reverse($fields));
		
		$page->content = $CONTENT;
	}

	function generate_list($parent, $level, $keys, $vals, $menu) {
			
		$rank = 0;	
		$space = "";
		for ($i = 0; $i < $level; $i++)
			$space .= "&nbsp;&nbsp;&nbsp;&nbsp;";
		while ($id = @array_shift($menu[$parent]['children'])) {
			$rank = $menu[$id]['rank'];
			array_push($keys, "$parent#$rank");
			array_push($vals, $space . "--hier--");
			array_push($keys, "xxx");
			array_push($vals, $space . $menu[$id]['menuname']);
			list($keys, $vals) = generate_list($id, ($level+1), $keys, $vals, $menu);
		}
		
		array_push($keys, "$parent#" . ($rank+1));
		array_push($vals, "$space--hier--");
		
		return array($keys, $vals);
	}


	/**
	 * dress up page content using skin stuff
	 */
	function page_inc_dress($page) {
		print_form($page, $page->content['form']);
	}
}

?>
